<template>
	<view class="pics">
		<!-- 可滚动视图区域。用于区域滚动。scroll-y:允许纵向滚动 -->
		<scroll-view class="left" scroll-y="true">
			<view v-for="(item,index) in categories" :key="index"  :class="{active:index==currentIndex?true:false}" @click="leftClickHandle(index)"
			>{{item.title}}</view>
		</scroll-view>
		<scroll-view class="right" scroll-y="true" >
			<view class="item" v-for="(item,index) in secondData" :key="index">
				<view class="title"><text>{{item.title}}</text></view>
				<view class="good">
					<image :src="item.img_url" @click="previewImg(item.img_url)"/>
					<text>三星x4</text>
				</view>
			</view>
			<text v-if="secondData.length==0">暂无数据</text>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				categories:[],
				currentIndex:0,
				secondData:[],
			}
		},
		onLoad() {
			this.getCategories()
		},
		methods: {
			async leftClickHandle(index){
				this.currentIndex=index
				//发送请求
				const res = await this.$myRequest({
					url:'/api/getImages.json'
				})
				this.secondData=res.data.message
			},
			async getCategories(){
				const res = await this.$myRequest({
					url:'/api/getCategories.json'
				})
				this.categories=res.data.message
				this.leftClickHandle(0)
			},
			previewImg(current){
				//得到一个新的数组，每一项为img的url
				const urls = this.secondData.map(item=>{
					return item.img_url
				})
				uni.previewImage({
					current,
					urls
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		height: 100%;//一整屏
	}
	.pics{
		height: 100%;//一整屏
		display: flex;
		background:#F1F1F1;
		.left{
			width: 200rpx;
			height: 100%;//左侧滚动条的宽度：占满屏
			border-right: 1px solid #eee;//整个left加有边框
			background:#FFFFFF;
			view{
				height:60px;
				line-height: 60rpx;
				color: #333333;
				text-align: center;
				font-size: 30rpx;
				border-bottom: 1px solid #eee;//每个view加下边框
			}
		}
		.right{
			width:530rpx;
			height:100%;
			margin:20rpx auto ;//左右留一点距离，居中
			.item{
				background: #FFFFFF;
				image{
					width: 200rpx;
					height: 200rpx;
					border-radius: 5px;//图片圆角
				}
				.title{
					background: #F1F1F1;
					font-size: 29rpx;
					line-height: 29rpx;
				}
			}
		}
		.active{//view被选中的背景色
			background: #F1F1F1;
			color: #fff;//文字颜色
		}
	}
</style>
